from myelin.Ns import MYELINS
from nodetypes.core.DocumentOrdering import OrderByKeys
from nodetypes.ntypes.Document.showBase.MML import Document, Transform


class Document(Document):
    
    containsKey = 'mContains_Grouping'
    groupingKey = u'groupings'
    
    #def createContentContainer(self, context, node, locale):
    #    node = super(Document,self).createContentContainer(
    #        context, node, locale
    #    )
    #    
    #    model = context.model
    #    root = context.defaults.document_root                        
    #                
    #    nodes = self.filterUngroupedNodes(
    #        context,
    #        [],
    #        model,
    #        locale
    #    )
    #    if nodes:
    #        self.createContainerNodes(
    #            context,
    #            node.xml_append(
    #                node.xml_create_element(
    #                    u'a:grouping', MYELINS,
    #                    attributes = {
    #                        u'role'         : u'container',
    #                        u'key'          : u'ungrouped-nodes',
    #                        u'count'        : unicode(len(nodes)),
    #                    }
    #                )
    #            ),
    #            OrderByKeys(
    #                model,
    #                nodes
    #            ),
    #            root,
    #            locale
    #        )
    #        
    #    return node
    #
    #def filterUngroupedNodes(self, context, nodes, model, locale):
    #    # filter only nodes that have matching acl
    #    model = self.getModelByMode(context, model)
    #            
    #    action = self.containsAction
    #    acls = context.acls.checkNodeAcls
    #    n_extend = nodes.extend
    #    
    #    # MAX DISPLAY LIMIT
    #    # if len(nodes) == max_display_limit:
    #    #   return
    #    # Should this be a user preference???
    #    
    #    # filter direct children nodes
    #    n_extend(
    #        n for n in model.mContains \
    #        if not n.mGrouping and \
    #            acls(n.rdf_domain, action)                
    #    )
    #            
    #    # if model IS A translation of another model,
    #    # then filter the other models matching translated nodes
    #    # of the children nodes 
    #    for t in model.mTranslations:
    #        for c in t.mContains:
    #            n_extend(
    #                n for n in c.mTranslation \
    #                if not n.mGrouping and \
    #                    locale in n.dcLanguage and \
    #                    acls(n.rdf_domain, action)
    #            )
    #    
    #    # if model HAS translated models,
    #    # then filter the other models matching translated nodes
    #    # of the children nodes                     
    #    #for t in model.mTranslation:
    #    #    for c in t.mContains:
    #    #        n_extend(
    #    #            n for n in c.mTranslation \
    #    #            if not n.mGrouping and \
    #    #                locale in n.dcLanguage and \
    #    #                acls(n.rdf_domain, action)
    #    #        )
    #
    #    return nodes


class Transform(Transform):
    
    newDocument = Document